﻿///***************************************************************
/// 시  스  템 : Updms.Prj
/// 단위시스템 : PRJ
/// 계      층 : Presentation Layer
/// 프로그램명 : Prj1010m
/// 파  일  명 : Prj1010m.aspx.cs
/// 설      명 : 프로젝트등록
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-08-06 송시명   최초 작성
/// CH002  2008-12-23 송시명   보고서사용여부(Report_Yn) 추가
///***************************************************************
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.Page;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;

using Updms.Prj.Exe;


namespace Updms.Prj
{
	public partial class Prj1010m : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Prj1010m";
		public static readonly string gs_path_info = "/updms/Prj/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Prj prj = new CommCode.Prj(LANG_SET);

		//페이징
		private int li_cur_page, li_row_count, li_st_page, li_ed_page;
		protected int li_page_size = 10;//GRID_PAGESIZE;// 한 페이지에 출력될 행의수

		//검색
		protected string kw1 = "";	// 시작일 ~부터
		protected string kw2 = "";	// 시작일 ~까지
		protected string kw3 = "";	// 프로젝트명
		protected string kw4 = "";	// 발주처

		//메뉴 선택값
		protected string gs_sm = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetWorkDiv();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{
			//엔터검색
			txt_search_start_dt_from.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_start_dt_to.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_subject.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_order_co.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
		}

		#endregion

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";
			if (Request.QueryString["page"] == null || int.Parse(Request.QueryString["page"]) == 1)
			{
				li_cur_page = 1;
				li_st_page = 0;
				li_ed_page = li_page_size;
			}
			else
			{
				li_cur_page = int.Parse(Request.QueryString["page"]);
				li_st_page = (int.Parse(Request.QueryString["page"]) - 1) * li_page_size;
				li_ed_page = li_st_page + li_page_size;
			}

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						if (String.IsNullOrEmpty(txt_search_start_dt_from.Text))
						{
							kw1 = "2003-01-01";	// 2003-01-01
							txt_search_start_dt_from.Text = kw1;
						}
						else
						{
							kw1 = txt_search_start_dt_from.Text.Trim();
						}
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						txt_search_start_dt_from.Text = kw1;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						if (String.IsNullOrEmpty(txt_search_start_dt_to.Text))
						{
							kw2 = DateUtil.GetYearEx(0, "yyyy-MM-dd");	// 오늘
							txt_search_start_dt_to.Text = kw2;
						}
						else
						{
							kw2 = txt_search_start_dt_to.Text.Trim();
						}
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						txt_search_start_dt_to.Text = kw2;
					}
					
					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = txt_search_subject.Text.Trim();
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						txt_search_subject.Text = kw3;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw4"]))	// url에 값이 없으면
					{
						kw4 = txt_search_order_co.Text.Trim();
					}
					else
					{
						kw4 = Server.UrlDecode(Request.QueryString["kw4"]);
						txt_search_order_co.Text = kw4;
					}
					break;
				case "SEARCH":
					kw1 = String.IsNullOrEmpty(txt_search_start_dt_from.Text) ? "2003-01-01" : txt_search_start_dt_from.Text.Trim();// 2003-01-01
					kw2 = String.IsNullOrEmpty(txt_search_start_dt_to.Text) ? DateUtil.GetYearEx(0, "yyyy-MM-dd") : txt_search_start_dt_to.Text.Trim();// 오늘
					kw3 = txt_search_subject.Text.Trim();
					kw4 = txt_search_order_co.Text.Trim();
					break;
			}
		}

		//검색키워드
		private void ShowKeywordLabel()
		{
			lbl_search_keyword.Text = kw1 + " ~ " + kw2 + " / " + kw3 + " / " + kw4;
		}

		#endregion

		#region 디테일 Method (SetNewPrjId, SetWorkDiv)

		//신규프로젝트아이디
		private void SetNewPrjId()
		{
			Prj1010m_NT _Prj1010m_NT = new Prj1010m_NT();
			lbl_prj_id.Text = _Prj1010m_NT.GetNewPrjId();
		}

		//계약구분(SI,SM)
		private void SetWorkDiv()
		{
			prj.SetWorkDiv(ddl_work_div);
		}

		#endregion

		#region CRUD (SelectProcess, InsertProcess, UpdateProcess, DeleteProcess, SearchProcess)

		//조회
		private void SelectProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();
			int li_page_count;

			//검색
			lo_dp.AddProperty("ls_start_dt_from", typeof(string), kw1);
			lo_dp.AddProperty("ls_start_dt_to", typeof(string), kw2);
			lo_dp.AddProperty("ls_subject", typeof(string), kw3);
			lo_dp.AddProperty("ls_order_co", typeof(string), kw4);

			using (Prj1010m_NT _Prj1010m_NT = new Prj1010m_NT())
			{
				li_row_count = _Prj1010m_NT.GetSearchCount(lo_dp);	// 검색된 행수
				li_page_count = (int)((li_row_count - 1) / li_page_size) + 1;	// 총페이지수 계산

				//모두보기/페이징
				lo_dp.AddProperty("li_st_page", typeof(Int32), (cb_list_all.Checked) ? 0 : li_st_page);
				lo_dp.AddProperty("li_ed_page", typeof(Int32), (cb_list_all.Checked) ? li_row_count : li_ed_page);
				lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);
				lbl_page.Visible = !cb_list_all.Checked;
				
				lo_ds.Merge(_Prj1010m_NT.SelectProcess(lo_dp));

				GridView1.DataSource = lo_ds;
				GridView1.DataBind();

				Pager pager = new Pager(this, ref lbl_page);
				pager.PageNavigation(
					li_cur_page,
					li_page_count,
					gs_path_info + "?sm=" + gs_sm + "&kw1=" + Server.UrlEncode(kw1) + "&kw2=" + Server.UrlEncode(kw2) + "&kw3=" + Server.UrlEncode(kw3) + "&kw4=" + Server.UrlEncode(kw4) + "&page=");

				//결과출력(키워드, 검색행수)
				ShowKeywordLabel();
				lbl_result_count.Text = li_row_count.ToString("#,###,##0");
			}
		}

		//저장
		private void InsertProcess()
		{
			using (Prj1010m_RT _Prj1010m_RT = new Prj1010m_RT())
			{
				int li_return = _Prj1010m_RT.InsertProcess(SetValue());
				string ls_msg_id = (li_return > 0) ? "MSG_INF_007" : "MSG_ERR_105";	//저장되었습니다:저장할 수 없습니다
				CallMsgBoxInAjax(ls_msg_id);

				SearchProcess();
			}
		}

		//수정
		private void UpdateProcess()
		{
			using (Prj1010m_RT _Prj1010m_RT = new Prj1010m_RT())
			{
				int li_return = _Prj1010m_RT.UpdateProcess(SetValue());
				string ls_msg_id = (li_return > 0) ? "MSG_INF_006" : "MSG_ERR_104";	//수정되었습니다:수정할 수 없습니다
				CallMsgBoxInAjax(ls_msg_id);

				SearchProcess();
			}
		}

		//삭제
		private void DeleteProcess(string ls_xml)
		{
			using (Prj1010m_RT _Prj1010m_RT = new Prj1010m_RT())
			{
				int li_return = _Prj1010m_RT.DeleteProcess(ls_xml);
				string ls_msg_id = (li_return > 0) ? "MSG_INF_004" : "MSG_ERR_103";	//삭제되었습니다:삭제할 수 없습니다
				CallMsgBoxInAjax(ls_msg_id);

				SearchProcess();
			}
		}

		//검색
		private void SearchProcess()
		{
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();
			ClearValue();
			SelectProcess();
			SetNewPrjId();
		}

		#endregion

		#region SetValue, ClearValue

		//SetValue
		private DataPack SetValue()
		{
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("Seq", typeof(string), lbl_pk.Text);
			lo_dp.AddProperty("Prj_Id", typeof(string),				lbl_prj_id.Text);
			lo_dp.AddProperty("Start_Dt", typeof(string), 			ConversionUtil.DefaultValue(txt_start_dt.Text));
			lo_dp.AddProperty("End_Dt", typeof(string), 			ConversionUtil.DefaultValue(txt_end_dt.Text));
			lo_dp.AddProperty("Subject", typeof(string), 			ConversionUtil.DefaultValue(txt_subject.Text));
			lo_dp.AddProperty("Work_Div", typeof(string), 			ddl_work_div.SelectedValue);
			lo_dp.AddProperty("Order_Co", typeof(string), 			ConversionUtil.DefaultValue(txt_order_co.Text));
			lo_dp.AddProperty("Main_Work", typeof(string), 			ConversionUtil.DefaultValue(txt_main_work.Text));
			lo_dp.AddProperty("Sw_Archi", typeof(string), 			ConversionUtil.DefaultValue(txt_sw_archi.Text));
			lo_dp.AddProperty("Hw_Archi", typeof(string), 			ConversionUtil.DefaultValue(txt_hw_archi.Text));
			lo_dp.AddProperty("Remark", typeof(string), 			ConversionUtil.DefaultValue(txt_remark.Text));
			lo_dp.AddProperty("Report_Yn", typeof(string),			rbl_report_yn.SelectedValue);
			lo_dp.AddProperty("Reporter", typeof(string),			ConversionUtil.DefaultValue(txt_reporter.Text));
			lo_dp.AddProperty("Report_Receiver", typeof(string),	ConversionUtil.DefaultValue(txt_report_receiver.Text));
			lo_dp.AddProperty("Report_Refer_Yn", typeof(string),	rbl_report_refer_yn.SelectedValue);
			lo_dp.AddProperty("Create_User", typeof(string),		USER_ID);
			lo_dp.AddProperty("Create_Dt", typeof(string),			"");
			lo_dp.AddProperty("Update_User", typeof(string),		USER_ID);
			lo_dp.AddProperty("Update_Dt", typeof(string),			"");

			return lo_dp;
		}

		//ClearValue
		private void ClearValue()
		{
			lbl_pk.Text = "";
			h_no.Value = "";
			lbl_prj_id.Text = "";
			txt_start_dt.Text = "";
			txt_end_dt.Text = "";
			txt_subject.Text = "";
			ddl_work_div.SelectedIndex = 0;
			txt_order_co.Text = "";
			txt_main_work.Text = "";
			txt_sw_archi.Text = "";
			txt_hw_archi.Text = "";
			txt_remark.Text = "";
			rbl_report_yn.SelectedIndex = 0;
			txt_reporter.Text = "";
			txt_report_receiver.Text = "";
			rbl_report_refer_yn.SelectedIndex = 0;
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
					//사용권한별 버튼처리
					PagePreRender.SetVisibleCtrl(this, access.CheckPgmUsingGrant(USER_DIV, PID), access.GetVisibleCtrl(PID));
				}
			}
			ChangePageMode();
		}

		//ChangePageMode
		private void ChangePageMode()
		{
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}
		
		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onmouseover", "overBackground('GridView1', " + (e.Row.RowIndex + 1) + ")");
				e.Row.Attributes.Add("onmouseout", "outBackground('GridView1', 1)");

				//번호
				e.Row.Cells[1].Text = ((li_row_count - ((li_cur_page - 1) * li_page_size)) - e.Row.RowIndex).ToString();

				//사용여부
				e.Row.Cells[15].Attributes.Add("class", (e.Row.Cells[15].Text == "Y") ? "color_darkred" : "color_gray");
				e.Row.Cells[18].Attributes.Add("class", (e.Row.Cells[18].Text == "Y") ? "color_darkred" : "color_gray");
			}
		}

		//RowCommand
		protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			GridView gv = (GridView)sender;
			GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

			LinkButton lbtn_rc = (LinkButton)GridView1.Rows[row.RowIndex].FindControl("lbtn_rc");

			//row.Cells[0].Text.ToString();//0.체크박스
			h_no.Value					= row.Cells[1].Text.ToString();										//1.번호
			lbl_pk.Text					= gv.DataKeys[row.RowIndex].Values["Seq"].ToString();				//2.Seq
			lbl_prj_id.Text				= row.Cells[3].Text.Trim();											//3.Prj_Id
			txt_start_dt.Text			= row.Cells[4].Text.Trim();											//4.Start_Dt
			txt_end_dt.Text				= row.Cells[5].Text.Trim();											//5.End_Dt
																											//6.Prj_Term
			txt_subject.Text			= lbtn_rc.Text;														//7.Subject (template)
			ddl_work_div.SelectedValue	= gv.DataKeys[row.RowIndex].Values["Work_Div"].ToString();			//8.Work_Div
																											//9.Work_Div_Nm
			txt_order_co.Text			= row.Cells[10].Text.Trim();											//10.Order_Co
			txt_main_work.Text			= gv.DataKeys[row.RowIndex].Values["Main_Work"].ToString();			//11.Main_Work
			txt_sw_archi.Text			= gv.DataKeys[row.RowIndex].Values["Sw_Archi"].ToString();			//12.Sw_Archi
			txt_hw_archi.Text			= gv.DataKeys[row.RowIndex].Values["Hw_Archi"].ToString();			//13.Hw_Archi
			txt_remark.Text				= gv.DataKeys[row.RowIndex].Values["Remark"].ToString();			//14.Remark
			rbl_report_yn.SelectedValue = row.Cells[15].Text.Trim();										//15.Report_Yn
			txt_reporter.Text			= gv.DataKeys[row.RowIndex].Values["Reporter"].ToString();			//16.Reporter
			txt_report_receiver.Text	= gv.DataKeys[row.RowIndex].Values["Report_Receiver"].ToString();	//17.Report_Receiver
			rbl_report_refer_yn.SelectedValue = row.Cells[18].Text.Trim();									//18.Report_Refer_Yn
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		//저장
		protected void ibtn_save_Click(object sender, EventArgs e)
		{
			if (global.CheckSaveMode("UPDMS_PRJ_SUMMARY", "Seq", lbl_pk.Text.Trim()))
			{
				UpdateProcess();
			}
			else
			{
				InsertProcess();
			}
		}

		//삭제
		protected void ibtn_delete_Click(object sender, EventArgs e)
		{
			StringBuilder sb = new StringBuilder();

			sb.Append("<UPDMS>");

			for (int i = 0; i < GridView1.Rows.Count; i++)
			{
				GridViewRow row = GridView1.Rows[i];
				HtmlInputCheckBox cb = (HtmlInputCheckBox)row.FindControl("cb_check");

				if (cb.Checked)
				{
					string ls_seq = GridView1.DataKeys[i].Values["Seq"].ToString();
					string ls_file_name = GridView1.DataKeys[i].Values["Attach_File1"].ToString();	//파일이름

					sb.Append("<UPDMS_PRJ_SUMMARY>");
					sb.Append("<Seq>" + ls_seq + "</Seq>");
					sb.Append("</UPDMS_PRJ_SUMMARY>");
				}
			}
			sb.Append("</UPDMS>");

			DeleteProcess(sb.ToString());
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_start_dt_from", typeof(string), kw1);
			lo_dp.AddProperty("ls_start_dt_to", typeof(string), kw2);
			lo_dp.AddProperty("ls_subject", typeof(string), kw3);
			lo_dp.AddProperty("ls_order_co", typeof(string), kw4);

			using (Prj1010m_NT _Prj1010m_NT = new Prj1010m_NT())
			{
				li_row_count = _Prj1010m_NT.GetSearchCount(lo_dp);

				lo_dp.AddProperty("li_st_page", typeof(Int32), 0);
				lo_dp.AddProperty("li_ed_page", typeof(Int32), li_row_count);
				lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

				lo_ds.Merge(_Prj1010m_NT.SelectProcess(lo_dp));
			}
			string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")";
			ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET)));
		}

		//모두보기
		protected void cb_list_all_CheckedChanged(object sender, EventArgs e)
		{
			SearchProcess();
		}

		#endregion
	}
}
